package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.mapper.CommentMapper;
import cn.tedu._04weibo.pojo.dto.CommentAddParam;
import cn.tedu._04weibo.pojo.entity.Comment;
import cn.tedu._04weibo.pojo.vo.CommentVO;
import cn.tedu._04weibo.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.rowset.JdbcRowSet;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "03-评论模块")
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @Operation(summary = "添加评论功能")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam, HttpSession session){
        log.debug("commentAddParam = " + commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //登录状态校验已经在过滤器中实现了
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam, comment);
        comment.setCreateTime(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insertComment(comment);

        return JsonResult.ok();
    }

    /**
     * 2.评论列表功能
     */
    @GetMapping("selectByWeiboId")
    @Operation(summary = "评论列表功能")
    @ApiOperationSupport(order = 20)
    public JsonResult selectByWeiboId(Long id){
        log.debug("id = " + id);
        List<CommentVO> commentVOS = commentMapper.selectByWeiboId(id);

        return JsonResult.ok(commentVOS);
    }
}








